/**
 * Request基础函数文件
 * 2018年11月15日 11点15分
 * nico@coser.pub
**/
let _pagejs = require('../_core/page.js')._builer({});
const config = require('_config.js');

_pagejs._ObjectEditor(Object);
/**----------------------------------------
 * -simplateRequest
 *    *builderCookie
 * 
 *    -appendHeaderHandler
 *        *[wx.showLoading]
 *    -appendHeaderHandler
 * 
 * -simplateRequest
 * ------------ While Requesr -------------
 * -appendCompleteHandler
 *    *setCookie
 *    -appendEndHandler
 *        *[wx.hideLoading]
 *    -appendEndHandler
 * -appendCompleteHandler
 *-----------------------------------------*/

/**
 * simplateRequest 构建请求体
 * @param object designRequest 设计请求
 * @param boolean needPut 是否需要put进data
 */
function simplateRequest(designRequest, needPut) {
  let result = JSON.parse(JSON.stringify(_defualtRequest));
  for (let x in designRequest) {
    if (x == "data" && needPut) { // 假如needPut为Ture编入data
      result[x] = { data: JSON.stringify(designRequest[x]) };
    } else if (x == "success" || x == "fail" || x == "complete") { // 添加成功回调其他操作
      result["_" + x] = designRequest[x];
    } else {
      result[x] = designRequest[x];
    }
  }

  result.header.Cookie = builderCookie(result.url.split("/")[2]);
  result.complete = function (res) {
    appendCompleteHandler(res, result);
  }
  appendStartHandler(result);
  return result;
}
module.exports.simplateRequest = simplateRequest;

function appendStartHandler(designRequest) {
  if (designRequest.silence) return;
  if (designRequest.topbarLoading) {
    wx.showNavigationBarLoading();
  } else {
    wx.showLoading({ title: '嗡嗡，通信中~', });
  }
}
function appendEndHandler(designRequest) {
  if (designRequest.silence) return;

  if (designRequest.topbarLoading) {
    wx.hideNavigationBarLoading();
  } else {
    wx.hideLoading();
  }
}


/**
 * appendCompleteHandler 处理返回内容体
 * @param object designRequest 设计请求
 * @param boolean needPut 是否需要put进data
 */
function appendCompleteHandler(res, designRequest) {
  let statusCode = res.statusCode;
  setCookieHandler(designRequest.url.split("/")[2], res.header['Set-Cookie']);
  appendEndHandler(designRequest);
  switch (statusCode) {
    case 200:               //或成功调用
      let result = res.data;
      if (result.status != 2) {
        designRequest._success(result.data);
      }
      break;
    case 500:               //或可处理错误

      break;
    case 404: case 403:     //不可处理错误

      break;
  }
}


const _defualtRequest = { // 预定义Request头
  // complete: appendCompleteHandler,  // 完成回调
  dataType: 'json',
  responseType: 'text',
  method: 'POST',
  header: {// Request头信息
    "Content-Type": "application/x-www-form-urlencoded",
    // Cookie: builderCookie(), // Cookie Support
  },
}
module.exports._defualtRequest = _defualtRequest;

function builderCookie(domain) {
  if (config.useCookie) {
    let _CookiesCache = wx.getStorageSync(config.cookieStorageName + "-" + domain) || {};
    console.log(1234);
    return Object.toSearilizeJson(_CookiesCache)
  } else return "";
}
module.exports.builderCookie = builderCookie;

function setCookieHandler(domain, setCookie) {
  if (!setCookie) return;//为空无操作

  let cookies = wx.getStorageSync(config.cookieStorageName + "-" + domain) || {};//获取本地cookie信息

  Object.mergeSearilizeJson(setCookie, cookies);//合并/修改cookie信息
  wx.setStorageSync(config.cookieStorageName + "-" + domain, cookies);//保存修改
}
module.exports.setCookieHandler = setCookieHandler;
